<script setup>
import { ref } from 'vue';
import define from '@/utils/define';
import { getUuid } from '@/utils/tool';
import { useUser } from '@/pinia-store/user';
const baseUrl = ref(import.meta.env.VITE_APP_BASE_API)

const submitLoading = ref(false)
const dataForm = ref({
  username: '',
  password: '',
  captcha: '',
  uuid: getUuid()
})

const changeCode = () => {
  dataForm.value.uuid = getUuid()
}

const loginFinish = async () => {
  submitLoading.value = true
  const isThrough = await useUser().login(dataForm.value)
  submitLoading.value = false
  if (!isThrough) {
    changeCode()
  }
}
</script>

<route lang="yaml">
  meta:
    layout: user-default
    title: '登录'
</route>

<template>
  <div class="container">
    <div class="remarkable animate__animated  animate__fadeInLeft">
      <div class="title">
        <img src="@/assets/images/logo.png" />
        <h1>
          <strong>{{ define.title }}</strong>
        </h1>
      </div>
      <div class="desc">
        <img src="@/assets/images/background/login-box.png" />
        <h4>开箱即用的中后台管理系统</h4>
        <p>输入您的个人详细信息开始使用</p>
      </div>
    </div>
    <div class="login-box animate__animated animate__fadeInDown">
      <h4>登录</h4>
      <a-form
        :model="dataForm"
        name="basic"
        @finish="loginFinish"
      >
        <a-form-item
          name="username"
          :rules="[{ required: true, message: '请输入账号' }]"
        >
          <a-input v-model:value.trim="dataForm.username" size="large" placeholder="请输入账号" />
        </a-form-item>

        <a-form-item
          name="password"
          :rules="[{ required: true, message: '请输入密码' }]"
        >
          <a-input-password v-model:value.trim="dataForm.password" size="large" placeholder="请输入密码" />
        </a-form-item>

        <a-form-item
          name="captcha"
          :rules="[{ required: true, message: '请输入验证码' }]"
        >
          <div class="code-box">
            <a-input style="flex: 1;" v-model:value.trim="dataForm.captcha" size="large" placeholder="请输入图形验证码" />
            <img :src="`${baseUrl}/captcha?uuid=${dataForm.uuid}`" @click="changeCode"/>
          </div>
        </a-form-item>

        <a-form-item>
          <a-button :loading="submitLoading" type="primary" block html-type="submit" size="large">登录</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  position: relative;
  display: flex;
  justify-content: space-around;
  align-items: center;
  &::before {
    content: '';
    width: 100%;
    height: 100%;
    position: absolute;
    left: -50%;
    top: 0;
    background: url('@/assets/images/background/login-bg.png') no-repeat;
    background-size: 200% 107%;
    background-position-x: 100%;
  }
  .remarkable {
    height: 100%;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .title {
      position: absolute;
      top: 20px;
      display: flex;
      align-items: center;
      h1 {
        margin-right: 8px;
        color: #ffffff;
        font-size: 24px;
      }
    }
    .desc {
      color: #ffffff;
      img {
        width: 460px;
      }
      h4 {
        margin: 26px 0 20px;
        font-size: 28px;
      }
      p {
        font-size: 16px;
      }
    }
  }
  .login-box {
    width: 550px;
    height: 360px;
    padding: 25px;
    border-radius: 15px;
    box-shadow: 2px 2px 10px 0 rgba(0, 0, 0,0.2);
    h4 {
      margin-bottom:22px;
      font-size: 28px;
      font-weight: bold;
    }
    :deep(.ant-form) {
      padding: 0 50px;
    }
    .code-box {
      width: 100%;
      display: flex;
      align-items: center;
      img {
        width: 120px;
        height: 45px;
        margin-left: 15px;
        border-radius: 15px;
        border: 1px solid #eeeeee;
      }
    }
  }
}
</style>